package com.saer.client.tab;

import com.saer.client.tab.visita.VisitaTab;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.events.CloseClickHandler;
import com.smartgwt.client.widgets.tab.events.TabCloseClickEvent;

public class DefaultTabset extends TabSet {

	private boolean showDefaultTab = false;
	private String defaultTabTitle;
	private Img defaultTabImg;
	private String visitaTabId = null;

	public DefaultTabset(String defaultTabTitle, Img defaultTabImg) {

		this.defaultTabTitle = defaultTabTitle;
		this.defaultTabImg = defaultTabImg;

		setSize("100%", "100%");
		setShowMoreTab(false);
		setCanCloseTabs(true);

		addCloseClickHandler(new CloseClickHandler() {
			public void onCloseClick(TabCloseClickEvent event) {
				if (event.getTab() instanceof VisitaTab) {
					((VisitaTab) event.getTab()).getMainLayout().setHasVisitaTab(false);
					((VisitaTab) event.getTab()).stopReload();
				}
				removeTab(event.getTab());
				event.cancel();
			}
		});

		showDefaultTab();
	}

	public void showDefaultTab() {
		Tab defaultTab = new Tab(defaultTabTitle);
		Img image = defaultTabImg;
		image.setLayoutAlign(Alignment.CENTER);
		// image.setLayoutAlign(VerticalAlignment.TOP);

		HLayout hLayout = new HLayout();
		// hLayout.setPadding(20);
		hLayout.setAlign(Alignment.CENTER);
		// hLayout.setLayoutAlign(VerticalAlignment.TOP);
		hLayout.setSize("100%", "100%");
		hLayout.addMember(image);

		defaultTab.setCanClose(false);
		defaultTab.setPane(hLayout);

		addTab(defaultTab);

		showDefaultTab = true;
	}

	@Override
	public void removeTab(Tab tab) {
		super.removeTab(tab);
		if (getTabs() != null && getTabs().length == 0) {
			showDefaultTab();
		}
	}

	@Override
	public void addTab(Tab tab) {
		if (showDefaultTab) {
			removeTab(0);
			showDefaultTab = false;
		}
		
		if (tab instanceof VisitaTab) {
			visitaTabId = tab.getID();
		}
		
		super.addTab(tab);
	}

	public String getVisitaTabId() {
		return visitaTabId;
	}

}
